//
//  myLayout.m
//  homeWorkForNews
//
//  Created by 苗钧豪 on 16/8/3.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "myLayout.h"

@implementation myLayout
- (instancetype)init {
    self = [super init];
    if (self) {
        _array = [NSMutableArray array];
    }
    return self;
}
#pragma mark ================ 必须实现的方法 ================
- (void)prepareLayout {
//    NSMutableArray *widthArray = [NSMutableArray array];
    self.contentWidth = 0.0f;
    CGFloat x = 5;
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i ++) {
        NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        CGFloat y = 0;
        CGFloat width = [self.delegate collectionView:self.collectionView layout:self height:50 heightForItemIndexPath:index];
        CGFloat height = 50;
//        CGFloat width = 50;
        //        widthArray[i] = @([widthArray[i] floatValue] + width);
//        self.contentWidth = MAX(self.contentWidth, [widthArray[i] floatValue]);
        attributes.frame = CGRectMake(x, y, width, height);
        x =  width + x + 5;
        [_array addObject:attributes];
        self.contentWidth = x;
    }
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _array;
}
- (CGSize)collectionViewContentSize {
    return CGSizeMake(self.contentWidth, 50);
}
@end
